﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using TecBaseObjects.Objects;


namespace TecData.Layer {
    internal class ProjectCaseDataLayer : BaseSqlDataLayer {

        #region Members

        private string loadProjectCaseStatement = @"Projects.GetProjectCases";

        #endregion

        #region Load

        public virtual ProjectCaseObject LoadProjectObject(int ProjectID) {
            using(IDataReader reader = this.GetDataReaderFromStoredProcedure(loadProjectCaseStatement, new SqlParameter("@ProjectID", ProjectID))) {
                return (reader.Read() ? LoadProjectCaseObjectFromDataReader(reader) : null);
            }
        }

        public virtual ProjectCaseObject LoadProjectObject(string MatchCode) {
            using(IDataReader reader = this.GetDataReaderFromStoredProcedure(loadProjectCaseStatement, new SqlParameter("@MatchCode", MatchCode))) {
                return (reader.Read() ? LoadProjectCaseObjectFromDataReader(reader) : null);
            }
        }

        #endregion

        #region Save
        #endregion

        #region Functions

        protected virtual ProjectCaseObject LoadProjectCaseObjectFromDataReader(IDataReader reader) {
            List<ProjectContactObject> myList = new List<ProjectContactObject>();

            ProjectCaseObject po = new ProjectCaseObject();
            po.ProjectID = this.GetInt32(reader, "ProjectID");
            po.ProjectNumber = this.GetInt16(reader, "ProjectNumber");
            po.Intern = this.GetBoolean(reader, "Intern");
            po.ProjectStatus = this.GetByte(reader, "ProjectStatus");
            po.MatchCode = this.GetString(reader, "MatchCode");
            po.CustomerName = this.GetString(reader, "CustomerName");
            po.Plz = this.GetString(reader, "Plz");
            po.Ort = this.GetString(reader, "Ort");
            po.Straße = this.GetString(reader, "Straße");
            po.StandortID = this.GetInt32(reader, "StandortID");
            po.StandortCode = this.GetString(reader, "StandortCode");
            po.CustomerSource = this.GetInt32(reader, "CustomerSource");
            po.FilePath = this.GetString(reader, "FilePath");
            po.CalculationFileName = this.GetString(reader, "CalculationFileName");
            po.Distance = (double)this.GetDecimal(reader, "Distance");
            po.FirstContactDate = this.GetDateTime(reader, "FirstContactDate");
            po.ImplementationDate = this.GetDateTime(reader, "ImplementationDate");
            po.OfferDate = this.GetDateTime(reader, "OfferDate");
            po.OrderDate = this.GetDateTime(reader, "OrderDate");
            po.FinishDateProjected = this.GetDateTime(reader, "FinishDateProjected");
            po.FinishDateReal = this.GetDateTime(reader, "FinishDateReal");

            reader.NextResult();

            while (reader.Read()) {
                ProjectContactObject con = new ProjectContactObject();
                con.ProjectID = this.GetInt32(reader, "ProjectID");
                con.ContactID = this.GetInt32(reader, "ContactID");
                con.IsOrganisation = this.GetBoolean(reader, "IsOrganisation");
                con.Name1 = this.GetString(reader, "Name1");
                con.Name2 = this.GetString(reader, "Name2");
                con.Sex = this.GetByte(reader, "Sex");
                con.ContactTypeID = this.GetInt32(reader, "ContactTypeID");
                con.ContactType = this.GetString(reader, "ContactType");
                con.AddressType = this.GetString(reader, "AddressType");
                con.LKZ = this.GetString(reader, "LKZ");
                con.Plz = this.GetString(reader, "Plz");
                con.Ort = this.GetString(reader, "Ort");
                con.Straße = this.GetString(reader, "Street");
                con.Telefon = this.GetString(reader, "Telefon");
                con.Fax = this.GetString(reader, "Fax");
                con.Mobil = this.GetString(reader, "Mobil");
                con.EMail = this.GetString(reader, "EMail");
                con.VATRegNo = this.GetString(reader, "VATRegNo");

                myList.Add(con);
            }
            po.MyContacts = myList;
            return po;
        }

        #endregion

    }
}
